package com.runa.monitor.platinterface.netservice.equipment.dtu;

import com.runa.monitor.comm.dto.Node;
import com.runa.monitor.platinterface.extend.equ.EquInstructService;
import com.runa.monitor.platinterface.mqtt.MqttBaseController;
import com.runa.monitor.platinterface.backwork.function.TaskFunction;
import com.runa.monitor.platinterface.netservice.connect.commstatus.CommStatus;
import com.runa.monitor.platinterface.netservice.equipment.NetEqu;
import com.runa.monitor.platinterface.netservice.equipment.dtu.point.ModbusPoints;
import com.runa.monitor.platinterface.task.RequestTask;
import com.runa.persistence.task.RequestPendingTask;
import com.runa.persistence.util.LogUtil;
import com.runa.protocol.dto.IRequestData;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.equdata.EquData;
import com.runa.rccprotocol.netservice.dtu.ModbusRtuResponse;
import com.runa.rccprotocol.netservice.modbus.ModbusRequest;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 2024/8/23 10:30
 * zhangYB
 */
@Slf4j
public class MqttModbusDriver extends ModbusDriver {

    Lock lock = new ReentrantLock();

    public MqttModbusDriver(String serial, Integer timeout, ModbusPoints points) {
        super(serial, timeout, points);
    }

    @Override
    public void readNodeEqu(List<Node> nodes, TaskFunction function, EquData.EquDataType type) {
        for (Node node : nodes) {
            readPoints(node, function);
        }
    }

    @Override
    public void send(RequestTask requestTask) {
        try {
            this.requestTask = requestTask;
            if (requestTask.getTaskFunction() instanceof FourGDtuTaskFunction && requestTask.getRequestData() instanceof ModbusRequest) {
                FourGDtuTaskFunction fourGDtuTaskFunction = (FourGDtuTaskFunction) requestTask.getTaskFunction();
                ModbusRequest modbusRequest = (ModbusRequest) requestTask.getRequestData();
                MqttBaseController.getConnector().sendMessage(fourGDtuTaskFunction.getTopic(), modbusRequest.getData());
                fourGDtuTaskFunction.addRequest(modbusRequest);
                this.lastTime = System.currentTimeMillis();
                sending = modbusRequest.isHaveResult();
                LogUtil.info(fourGDtuTaskFunction.getSerial(), "sending: {} topic: {}", sending, fourGDtuTaskFunction.getTopic());
                LogUtil.info(fourGDtuTaskFunction.getSerial(), requestTask.getRequestData().getData(), "发送");
                EquInstructService.start(fourGDtuTaskFunction.getSerial(), requestTask.getRequestData());
            }
        } catch (Exception e) {
            log.error("MqttModbusDriver.send: ", e);
        }
    }

    public void sendRequest(String serial, IRequestData request, TaskFunction function, String topic) {
        lock.lock();
        try {

        } catch (Exception e) {
            log.error("MqttModbusDriver.sendRequest: ", e);
        } finally {
            lock.unlock();
        }
    }

//    public void sendRequest(String topic, IRequestData request, TaskFunction function) {
//        lock.lock();
//        try {
//            this.function = function;
//            MqttBaseController.getConnector().sendMessage(topic, (byte[])request.getData());
//            if (request.isHaveResult()) {
//                if (!mockLock(request)) {
//                    if (retrySign >= maxRs) {
//                        function.doApply(CommStatus.TimeOut, null);
//                    } else {
//                        this.retrySign++;
//                        sendRequest(topic, request, function);
//                    }
//                }
//            } else if (function != null) {
//                function.doApply(CommStatus.OK, null);
//            }
//        } catch (Exception ignored) {
//        } finally {
//            this.retrySign = 0;
//            lock.unlock();
//        }
//    }

    public void readPoints(Node node, TaskFunction function) {

    }

    public void readPoints(Integer slaveId, TaskFunction function) {
    }



}
